package com.rtx.wms.std.sku.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.common.bean.Codelkup;
import com.rtx.wms.std.common.bean.WmsConstant;
import com.rtx.wms.std.sku.bean.dto.SkuDTO;
import com.rtx.wms.std.sku.bean.po.Sku;
import com.rtx.wms.std.sku.mapper.SkuConfigMapper;
import com.rtx.wms.std.sku.mapper.SkuMapper;
import com.rtx.wms.std.sku.service.SkuService;
import com.rtx.wms.std.validation.sku.SkuValidation;

@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService{
	
	@Autowired
	SkuValidation skuValidation;
	
	@Autowired
	SkuConfigMapper skuConfigMapper;
	

	@Override
	public Sku dtoConvertToPo(SkuDTO dto) {
		Sku po = new Sku();
		BeanUtils.copyProperties(dto, po);
		po.setClassName(dto.getRtxsplb());
		return po;
	}
	
	@DataSourceAnnotation
	public void handleSKUStorerkey(String datasourcekey,Sku sku) {
		String storerkey = sku.getStorerkey();
        //获取系统代码【RTXSHIPSTORE】，使用店仓编号匹配代码获取仓库编号、货主编号
        LambdaQueryWrapper<Codelkup> codelkupQuery = Wrappers.lambdaQuery();
        codelkupQuery.eq(Codelkup::getCode, storerkey).eq(Codelkup::getListname, "RTXSHIPSTORE");
        Codelkup rtxshipstore = skuValidation.validationCodelkup(WmsConstant.ENTERPRISE, codelkupQuery, true);
        sku.setStorerkey(rtxshipstore.getUdf3());
        sku.setWhseid(rtxshipstore.getUdf2());
	}

	@DataSourceAnnotation
	@Override
	public void addSku(String datasourcekey, Sku sku) {
		if(!StringUtils.hasLength(sku.getSku())) {
			return;
		}
		LambdaQueryWrapper<Sku> lqw = Wrappers.lambdaQuery();
		lqw.eq(Sku::getSku, sku.getSku());
		lqw.eq(Sku::getStorerkey, sku.getStorerkey());
		boolean exists =  baseMapper.exists(lqw);
		if(exists) {
			UpdateWrapper<Sku> where = new UpdateWrapper<>();
			where.eq("sku", sku.getSku());
			where.eq("storerkey", sku.getStorerkey());
			sku.setSerialkey(null);
			sku.setEdiflag("P");
			sku.setMsg("");
			sku.setWhseid(datasourcekey.toUpperCase());
			baseMapper.update(sku, where);
		}else {
			baseMapper.insert(sku);
		}
	}

	@DataSourceAnnotation
	@Override
	public List<Sku> selectSkuByWrapper(String datasourcekey, LambdaQueryWrapper<Sku> lqw) {
		return baseMapper.selectList(lqw);
	}
	
	@DataSourceAnnotation
	@Override
	public void addConfigSku(String datasourcekey, Sku sku) {
		if(!StringUtils.hasLength(sku.getSku())) {
			return;
		}
		LambdaQueryWrapper<Sku> lqw = Wrappers.lambdaQuery();
		lqw.eq(Sku::getSku, sku.getSku());
		lqw.eq(Sku::getStorerkey, sku.getStorerkey());
		boolean exists =  baseMapper.exists(lqw);
		if(exists) {
			Sku where = new Sku();
			where.setSku(sku.getSku());
			where.setStorerkey(sku.getStorerkey());
			
			sku.setSerialkey(null);
			sku.setStorerkey(null);
			sku.setSku(null);
			sku.setEdiflag("P");
			sku.setMsg("");
			sku.setWhseid(datasourcekey.toUpperCase());
			skuConfigMapper.rtxconfigUpdate(sku, where);
		}else {
//			baseMapper.insert(sku);
			skuConfigMapper.rtxconfigColumnsSave(sku);
		}
	}
	
}
